#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>

#define N 100
#define NUM_THREADS 10

float a [N][N];
float b [N][N];
float c [N][N];

struct v {
   int i; 
   int j; 
};

void *runner(void *param);

int main(int argc, char *argv[]) {

   int i,j, count = 0;
   clock_t tStart = clock();
   
   for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            a[i][j] = (float)i + j;
            b[i][j] = (float)i - j;
            c[i][j] = 0.0f;
        }
    }
   
   for(i = 0; i < N; i++) {
      for(j = 0; j < N; j++) {
         //Assign a row and column for each thread
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         /* Now create the thread passing it data as a parameter */
         //pthread_t tid;       //Thread ID
         //pthread_attr_t attr; //Set of thread attributes
         //Get the default attributes
         //pthread_attr_init(&attr);
         //Create the thread
         //pthread_create(&tid,&attr,runner,data);
         runner(data);
         //Make sure the parent waits for all thread to complete
         //pthread_join(tid, NULL);
         count++;
      }
   }

   //Print out the resulting matrix
   for(i = 0; i < N; i++) {
      for(j = 0; j < N; j++) {
         std::cout << c[i][j];
      }
      std::cout << "\n";
   }
   std::cout << "Execution time= ";
   std::cout << (double)(clock() - tStart)/CLOCKS_PER_SEC;
   return 0;
}

//The thread will begin control in this function
void *runner(void *param) {
   struct v *data = (struct v *)param; // the structure that holds our data
   int n=0;
   float sum = 0; //the counter and sum

   //Row multiplied by column
   for(n = 0; n< N; n++){
      sum += a[data->i][n] * b[n][data->j];
   }
   //assign the sum to its coordinate
   c[data->i][data->j] = sum;

   //Exit the thread
   //pthread_exit(0);

   return 0;
}